using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;


namespace Labirinto_Magico
{
    public class PlayerChoiceMenu : Microsoft.Xna.Framework.DrawableGameComponent
    {
        List<GUIButton> options = null;
        RenderingData mRender = null;
        SoundHandler mSound = null;
        GUILabel mLabelPlayers = null;

        int mPlayerCounter = 1;

        public PlayerChoiceMenu(Game game)
            : base(game)
        {
            options = new List<GUIButton>();
            mRender = (RenderingData)Game.Services.GetService(typeof(RenderingData));
            mSound = (SoundHandler)Game.Services.GetService(typeof(SoundHandler));
        }

        public override void Initialize()
        {
            int halfWidth = Game.GraphicsDevice.Viewport.Width / 2;
            int heightOnFour = Game.GraphicsDevice.Viewport.Height / 4;
            int widthOnFour = halfWidth / 2;

            Vector2 buttonRender = new Vector2(widthOnFour, heightOnFour);

            options.Add(new GUIButton("PLUS", new Vector2(widthOnFour * 3, heightOnFour), buttonRender));
            options.Add(new GUIButton("MINUS", new Vector2(widthOnFour, heightOnFour), buttonRender));
            options.Add(new GUIButton("MAIN", new Vector2(halfWidth, heightOnFour * 3 + 10), buttonRender));
            options.Add(new GUIButton("NEW", new Vector2(halfWidth, heightOnFour * 2 + 10), buttonRender));

            Vector2 labelRender = new Vector2(widthOnFour, heightOnFour);
            mLabelPlayers = new GUILabel("NPlayers", new Vector2(halfWidth, heightOnFour), labelRender);

            base.Initialize();
        }

        public override void Update(GameTime gameTime)
        {
            if (options.ElementAt(2).PressButton())
            {
                mSound.ButtonSound();
                Game.Components.Remove(this);
                Game.Components.Add(new MainMenu(Game));
            }
            else if (options.ElementAt(3).PressButton())
            {
                mSound.ButtonSound();
                Game.Components.Remove(this);
                Game.Components.Add(new GameLogic(Game, LOAD_FROM.None, mPlayerCounter));
            }
            else if (mPlayerCounter < 3 && options.ElementAt(0).PressButton())
            {
                mSound.ButtonSound();
                ++mPlayerCounter;
            }
            else if (mPlayerCounter > 1 && options.ElementAt(1).PressButton())
            {
                mSound.ButtonSound();
                --mPlayerCounter;
            }         

            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            foreach (GUIButton option in options)
            {
                Texture2D tex = new Texture2D(GraphicsDevice, 32, 32);
                float rotation = 0;
                switch (option.mType)
                {
                    case "PLUS":
                        if (option.IsMouseOver())
                            tex = mRender.mTextureData.arrowSelected;
                        else
                            tex = mRender.mTextureData.arrow;
                        rotation = (float)(Math.PI * 0.5);
                        break;

                    case "MINUS":
                        if (option.IsMouseOver())
                            tex = mRender.mTextureData.arrowSelected;
                        else
                            tex = mRender.mTextureData.arrow;
                        rotation = (float)(Math.PI * 1.5);
                        break;

                    case "MAIN":
                        if (option.IsMouseOver())
                            tex = mRender.mTextureData.mainMenuSelected;
                        else
                            tex = mRender.mTextureData.mainMenu;
                        break;
                    case "NEW":
                        if (option.IsMouseOver())
                            tex = mRender.mTextureData.newGameSelected;
                        else
                            tex = mRender.mTextureData.newGame;
                        break;
                }
                mRender.DrawTexture(tex, option.mDimension, option.mPosition, rotation, Color.White);
            }

            mRender.DrawTexture(mRender.mTextureData.playerScore, mLabelPlayers.mDimension, mLabelPlayers.mPosition, 0, Color.White);
            mRender.DrawFont(mRender.mTextureData.scoreFont, mLabelPlayers.mPosition, "" + mPlayerCounter, Color.Black, 1);

            base.Draw(gameTime);
        }
    }
}
